package javaCode.chapter14.generic;

import java.util.*;

/**
 * @author 徐州城
 * @version 1.0
 */
public class GenericExercise {
    public static void main(String[] args) {

        Person_ p1 = new Person_("小王", 20);
        Person_ p2 = new Person_("小宋", 22);
        Person_ p3 = new Person_("小金", 35);

        //使用泛型的方式给HashMap 放入3个对象
        HashSet<Person_> hashSet = new HashSet<Person_>();
        hashSet.add(p1);
        hashSet.add(p2);
        hashSet.add(p3);

        Iterator<Person_> iterator1 = hashSet.iterator();
        while (iterator1.hasNext()) {
            Person_ next = iterator1.next();
            System.out.println(next);
        }

        for (Person_ p : hashSet) {
            System.out.println(p);
        }


        HashMap<String, Person_> hashMap = new HashMap<String, Person_>();
        /*
            public class HashMap<K,V> {}
         */

        hashMap.put(p1.getName(),p1);
        hashMap.put(p2.getName(),p2);
        hashMap.put(p3.getName(),p3);

        Set<Map.Entry<String, Person_>> entries = hashMap.entrySet();
        Iterator<Map.Entry<String, Person_>> iterator = entries.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Person_> next = iterator.next();
            System.out.println(next.getKey() + "-" + next.getValue());
        }

        Set<String> strings = hashMap.keySet();
        for (Object o : strings) {
            System.out.println("-" + hashMap.get(o));
        }

    }
}

/*
练习：
1. 创建 3个学生对象
2. 放入到 HashSet中学生对象使用
3. 放入到 HashMap中，要求Key 是String name,Value 是学生对象
4. 使用两种遍历方式
 */
class Person_{
    private String name;
    private int age;

    public Person_(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person_{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}